package com.st.bottomnavigation.ui.navbar;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.hjy.bluetooth.HBluetooth;
import com.st.bottomnavigation.R;
import com.st.bottomnavigation.ui.activity.GroupActivity;
import com.st.bottomnavigation.ui.activity.TaskPhotoActivity;
import com.st.bottomnavigation.utils.HttpClientUtils;
import com.st.bottomnavigation.utils.LoadingDialog;

public class DiscoverFragment extends Fragment  {
    private static final String TAG = "MyFragment";
    private static DiscoverFragment discoverFragment;
    private View rootView;
    private Button takePhoto;
    private Button chuji,send;

    public DiscoverFragment() {
    }
    //单例创建
    public static DiscoverFragment getInstance(){
        if (discoverFragment == null) return new DiscoverFragment();
        Log.d("test", "onCreateView:DiscoverFragment ");
        return discoverFragment;
    }
    @Override
    public void onDetach() {
        super.onDetach();
        Log.d("MyFragment", "onDetach: DiscoverFragment");
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null)
            rootView = inflater.inflate(R.layout.fragment_discover, container, false);
        HBluetooth.init(getContext());
        initView();
        listener();
        return rootView;
    }
    private void initView() {
//        TextView title = rootView.findViewById(R.id.title);
//        title.setText("发现");
        takePhoto = rootView.findViewById(R.id.takePhoto);
        chuji = rootView.findViewById(R.id.chuji);
        send = rootView.findViewById(R.id.send);
        //开启蓝牙功能
        HBluetooth.getInstance().enableBluetooth();

    }
    private LoadingDialog dialog;//加载弹框
    private Handler mHandler = new Handler();
    private void listener() {

        chuji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), GroupActivity.class);
                startActivity(intent);
            }
        });

        takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), TaskPhotoActivity.class);
                startActivity(intent);
            }
        });

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //加载弹窗
                LoadingDialog loadingDialog = new LoadingDialog.Builder(getActivity())
                        .setMessage("加载中...")
                        .setCancelable(true)//返回键是否可关闭
                        .setCancelOutside(false)//点击弹框外是否可关闭
                        .create();
                //显示
                loadingDialog.show();

                //模拟异步发送请求后关闭加载弹窗
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //关闭显示
                        loadingDialog.dismiss();
                    }
                }, 1000);
                // HttpClientUtils.getTest(handler, condition);
            }
        });

    }

    /**
     * handler处理回调数据
     */
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                //腾讯逆地址解析
                case HttpClientUtils.MSG_GET_SUCCESS:{
                    JSONObject data = (JSONObject) msg.obj;
                    Log.d(TAG, "data: "+data);
                    break;
                }


            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: DiscoverFragment");
    }
}